package com.smxy.mall.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.smxy.mall.dao.AddressDao;
import com.smxy.mall.dao.GoodsDao;
import com.smxy.mall.dao.OrderDao;
import com.smxy.mall.dao.UserDao;
import com.smxy.mall.entity.Goods;
import com.smxy.mall.entity.Order;
import com.smxy.mall.model.Current;
import com.smxy.mall.model.request.OrderReq;
import com.smxy.mall.service.OrderService;
import com.smxy.mall.utils.Common;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class OrderServiceImpl implements OrderService {
    @Resource
    private OrderDao orderDao;
    @Resource
    private GoodsDao goodsDao;
    @Resource
    private AddressDao addressDao;
    @Resource
    private UserDao userDao;

    /**
     * 订单入库
     * @param orderReq
     * @param current
     * @return
     */
    @Override
    public Order add(OrderReq orderReq, Current current) {
        Order order = new Order();
        Gson gson=new Gson();
        //商品信息列表
        List<Integer> goodsIds= gson.fromJson(orderReq.getGoodsId(), new TypeToken<List<Integer>>() {}.getType());
        List<Object> goodsList = new ArrayList<>();
        final Double[] goodsMoneyAll = {0.00};
        goodsIds.forEach(item -> {
            Goods obj = goodsDao.findById(item);
            goodsMoneyAll[0] += obj.getPrice();
            goodsList.add(obj);
        });
        //配送金额
        Double sendMoney = 0.00;
        if(Common.isId(orderReq.getAddressId().toString()) && orderReq.getOrderType()==1){
            order.setAddressInfo(gson.toJson(addressDao.findById(orderReq.getAddressId())));
            sendMoney = 10.00;
        }
        //优惠金额
        Double discountMoney = 0.00;
        if(Common.isId(orderReq.getCouponId().toString())){
            sendMoney = 2.00;
        }
        Double orderMoney = goodsMoneyAll[0]+sendMoney; //订单总金额
        Double payMoney = orderMoney - discountMoney; //支付金额

        //金额信息
        order.setOrderMoney(orderMoney);
        order.setPayMoney(payMoney);
        order.setSendMoney(sendMoney);
        order.setDiscountMoney(discountMoney);
        //订单基本信息
        order.setUserId(current.getUserId());
        order.setUserInfo(gson.toJson(userDao.findById(current.getUserId())));
        order.setGoodsId(orderReq.getGoodsId());
        order.setGoodsList(gson.toJson(goodsList));
        order.setCouponId(orderReq.getCouponId());
        order.setAddressId(orderReq.getAddressId());
        order.setStatus(0);
        order.setOrderType(orderReq.getOrderType());
        order.setOrderTypeName(orderReq.getOrderType()==1 ? "配送" : "自取");
        order.setPayType(orderReq.getPayType());
        order.setPayTypeName(orderReq.getPayType()==1 ? "微信支付" : "其他");
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
        order.setOrderNo("V"+df.format(new Date()));
        return orderDao.save(order);
    }

    /**
     * 订单详情
     * @param id
     * @return
     */
    @Override
    public Order detail(Integer id) {
        return orderDao.findById(id);
    }

    @Override
    public List<Order> list() {
        return orderDao.findAll();
    }

    @Override
    public Page<Order> listNow(Integer shopId, Integer userId, Pageable pageable) {
        return orderDao.findByShopIdAndUserIdAndStatusLessThan(shopId,userId,4,pageable);
    }

    @Override
    public Page<Order> listAll(Integer shopId, Integer userId, Pageable pageable) {
        return orderDao.findByShopIdAndUserId(shopId,userId,pageable);
    }

    @Override
    public Page<Order> listPage(Integer shopId, Integer userId, Integer status, Pageable pageable) {
        return orderDao.findByShopIdAndUserIdAndStatus(shopId,userId,status,pageable);
    }

//    @Override
//    public List<Order> listAll(Integer userId) {
//        return orderDao.findByUserId(userId);
//    }
//
//    @Override
//    public List<Order> listStatus(Integer status, Integer userId) {
//        return orderDao.findByStatusAndUserId(status, userId);
//    }
}
